Skip to content

Add bounded-memory training and colocation controls#1890

Merged
erictang000 merged 6 commits into
NovaSky-AI:mainfrom
YJHMITWEB:memory-efficiency-patches
Jul 21, 2026
Merged

Add bounded-memory training and colocation controls#1890
erictang000 merged 6 commits into
NovaSky-AI:mainfrom
YJHMITWEB:memory-efficiency-patches

Conversation

@YJHMITWEB

@YJHMITWEB YJHMITWEB commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Bounded-Memory Megatron Training and Colocation

Summary

This PR adds controls that bound temporary training memory and make colocated Megatron offload behavior complete.

Changes

Change Explanation
CPU-resident policy microbatches Keeps the DP-sharded policy batch on CPU and transfers one nested microbatch to CUDA immediately before its forward step.
Bounded vocabulary entropy Chunks Megatron vocabulary entropy by an explicit size or an automatically calculated memory budget, and skips fully masked chunks.

CPU-resident policy microbatches is opt-in; automatic vocabulary-entropy chunk sizing, which defaults to a 512 MiB temporary-memory budget.

Memory Validation

Variant Memory Spike Outcome
Earlier configuration 9.3 GiB OOM easily as the context/vocab size gets larger
Bounded-memory configuration 0.5 GiB All memory spikes are under control

@jinghanyao1-hub jinghanyao1-hub self-assigned this Jul 13, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces several memory optimization features, including CPU-resident microbatches, sequence-dimension chunking for Megatron vocab entropy computation, and a colocated worker memory barrier with optional hard-eviction for inactive reference workers. The review feedback suggests mapping PyTorch device indices to physical GPU indices via CUDA_VISIBLE_DEVICES to ensure accurate NVML memory statistics, and removing a fragile .squeeze(0) call on entropy chunks to robustly support varying input dimensions.

Comment thread skyrl/backends/skyrl_train/workers/worker.py Outdated
Comment thread skyrl/backends/skyrl_train/distributed/megatron/model_utils.py Outdated
Comment thread skyrl/backends/skyrl_train/workers/worker.py Outdated
Comment thread skyrl/backends/skyrl_train/distributed/megatron/model_utils.py Outdated
@NovaSky-AI NovaSky-AI deleted a comment from gemini-code-assist Bot Jul 13, 2026
@NovaSky-AI NovaSky-AI deleted a comment from gemini-code-assist Bot Jul 13, 2026
@YJHMITWEB
YJHMITWEB force-pushed the memory-efficiency-patches branch from 9b6a19a to 8af9294 Compare July 16, 2026 18:39
@YJHMITWEB
YJHMITWEB force-pushed the memory-efficiency-patches branch from 8af9294 to 782a8c7 Compare July 17, 2026 00:26
@erictang000 erictang000 self-assigned this Jul 20, 2026

@erictang000 erictang000 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just some minor comments

Comment thread skyrl/backends/skyrl_train/workers/megatron/megatron_model_wrapper.py Outdated
Comment thread skyrl/backends/skyrl_train/workers/worker.py Outdated
return torch.cat(entropy_chunks, dim=-1)


def vocab_parallel_entropy_weighted_sum(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to confirm these changes are stable, can you run a basic gsm8k example before and after this PR, and link it here? Just want to check there is no significant speed regression from chunking entropy calculation, and that the entropy metric is exactly matching before

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a unit test on this part: tests/backends/skyrl_train/distributed/test_vocab_entropy_chunking.py, this verifies that the chunked CE is numerically identical. For the overhead, the following table shows:

chunk_memory_mb tokens/chunk #chunks exact op time vs unchunked
off (unchunked) 6.3 ms 1.0×
4096 4096 2 6.4 ms 1.0×
2048 2048 4 7.5 ms 1.2×
1024 1024 8 14.9 ms 2.4×
512 (current default) 512 16 29.7 ms 4.7×
256 256 32 59.6 ms 9.4×
128 128 64 118.6 ms 18.8×

The slowdown is expected, but since the overall step time here is at most hundreds of ms, the impact of chunking is almost negligible.

PPORayActorGroup stored `pg` and `num_gpus_per_actor` on self but nothing ever
read them back (the constructor already passes both directly to
_initiate_actors). Drop the two dead assignments; the constructor params are
unchanged.
Make the previously opt-in `cpu_resident_microbatches` behavior the only path
and remove the config flag. Each microbatch is now moved to the device just
before its forward step (in MegatronModelWrapper.forward_step), instead of
moving the whole mini-batch up front. This caps resident input-tensor memory at
one microbatch and is a no-op when a tensor is already on device.

The per-microbatch transfer is the single choke point for every forward and
forward-backward pass, so the up-front `.to(device)` calls in the worker are
removed; nothing downstream reads the mini-batch tensors on-device after the
microbatch loop (only chunk()/BatchIterator, which slice on CPU, and metadata).

Removes the MegatronConfig field, the wrapper constructor param, the worker
plumbing, and the CLI-override test for the flag.
@YJHMITWEB
YJHMITWEB force-pushed the memory-efficiency-patches branch from 1d15433 to 547d120 Compare July 21, 2026 18:55

@erictang000 erictang000 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, looking good thanks!

@erictang000
erictang000 merged commit d5bbe8c into NovaSky-AI:main Jul 21, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants